package com.compomics.proteocloud.model;

import com.compomics.proteocloud.db.accessor.Spectrumfile;
import com.compomics.proteocloud.io.MascotGenericFile;
import com.compomics.proteocloud.starter.CloudTools;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * Created with IntelliJ IDEA.
 * User: tmuth
 * Date: 14.08.12
 * Time: 16:45
 * To change this template use File | Settings | File Templates.
 */
public class PeptideSpectrumMatch extends SpectrumMatch {

    /**
     * The ion charge.
     */
    private int charge;

    /**
     * The actual spectrum file.
     */
    private MascotGenericFile mgf;

    /**
     * The search engine hits.
     */
    private List<SearchHit> searchHits;

    /**
     * Default empty constructor.
     */
    public PeptideSpectrumMatch() {

    }

    /**
     * Constructor for the PeptideSpectrumMatch.
     * @param spectrumid
     * @param searchHit
     */
    public PeptideSpectrumMatch(long spectrumid, SearchHit searchHit) {
        this.spectrumId = spectrumid;
        this.searchHits = new ArrayList<SearchHit>();
        this.searchHits.add(searchHit);
        this.charge = (int) searchHit.getCharge();
    }

    /**
     * Returns the search hit.
     * @return The search hit.
     */
    public SearchHit getFirstSearchHit() {
        return searchHits.get(0);
    }

    /**
     * Returns the list of search hits.
     * @return The list of search hits.
     */
    public List<SearchHit> getSearchHits() {
        return searchHits;
    }

    /**
     * Adds a search engine hit to the PSM. Checks for redundancy.
     * @param hit Another search engine hit to be added.
     */
    public void addSearchEngineHit(SearchHit hit) {
        if (!searchHits.contains(hit)) {
            this.searchHits.add(hit);
        }
    }
    /**
     * Returns the votes.
     * @return The number of votes for the search engine hits.
     */
    public int getVotes(){
        return searchHits.size();
    }

    /**
     * Returns the PSM charge.
     * @return The PSM charge.
     */
    public int getCharge() {
        return charge;
    }

    public MascotGenericFile getMascotGenericFile() throws SQLException, IOException {
        // Add spectrum file to the match.
        if(mgf == null){
            this.mgf = CloudTools.getInstance().getSpectrumFromSpectrumID(spectrumId);
        }
        return mgf;
    }
}

